import { literal } from 'sequelize';

import { REST } from '@donnu-prod-practice/api-types';
import { SortDirection } from '@donnu-prod-practice/types';

import { models } from '../../db';
import { NotFoundError } from '../../error';
import { ControllerType } from '../../types/types';

/* -------------------------------------------------------------------------- */
/*                                 CONTROLLER                                 */
/* -------------------------------------------------------------------------- */

export const getTask3CountryController: Controller = async (req, res, _nest) => {
  const countryList = await selectCountryList();

  const country1 = countryList[0];
  if (!country1) throw new NotFoundError('первая страна не найдена');

  const country2 = countryList[1];
  if (!country2) throw new NotFoundError('вторая страна не найдена');

  return res.json(
    prepareResponse(
      countryList,
      country1.residentRequests + country1.nonResidentRequests,
      country2.residentRequests + country2.nonResidentRequests
    )
  );
};

/* -------------------------------------------------------------------------- */
/*                                / CONTROLLER                                */
/* -------------------------------------------------------------------------- */

/* -------------------------------------------------------------------------- */
/*                                   QUERIES                                  */
/* -------------------------------------------------------------------------- */

const selectCountryList = () =>
  models.Country.Model.findAll({
    order: [[literal('countries.residentRequests + countries.nonResidentRequests'), SortDirection.desc]],
  });

/* -------------------------------------------------------------------------- */
/*                                  / QUERIES                                 */
/* -------------------------------------------------------------------------- */

/* -------------------------------------------------------------------------- */
/*                                TRANSFORMERS                                */
/* -------------------------------------------------------------------------- */

const prepareResponse = (
  countryList: ReadonlyArray<models.Country.Type>,
  allRequests1: number,
  allRequests2: number
): Response => countryList.map(prepareCountry(allRequests1, allRequests2));

/* -------------------------------------------------------------------------- */

const prepareCountry =
  (allRequests1: number, allRequests2: number) =>
  ({ id, name, rate, residentRequests, nonResidentRequests }: models.Country.Type): Response[number] => ({
    id,
    name,
    rate,
    residentRequests,
    nonResidentRequests,
    percent1: 100 - ((residentRequests + nonResidentRequests) * 100) / allRequests1,
    percent2: 100 - ((residentRequests + nonResidentRequests) * 100) / allRequests2,
  });

/* -------------------------------------------------------------------------- */
/*                               / TRANSFORMERS                               */
/* -------------------------------------------------------------------------- */

/* -------------------------------------------------------------------------- */
/*                                    TYPES                                   */
/* -------------------------------------------------------------------------- */

type Params = REST.country.getTask3.Params;
type Query = REST.country.getTask3.Query;
type Body = REST.country.getTask3.Body;
type Response = REST.country.getTask3.Response;

type Controller = ControllerType<Params, Query, Body, Response>;

/* -------------------------------------------------------------------------- */
/*                                   / TYPES                                  */
/* -------------------------------------------------------------------------- */
